home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / TURBOPASCAL WIN / PAINT.PAK / TOOLBAR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-06-08  |  4.1 KB  |  132 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal for Windows: Paint Demo         }
  4. {   ToolBar unit                                 }
  5. {   Copyright (c) 1992 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. unit ToolBar;
  10.  
  11. { This unit defines a tool bar window for the paint program.
  12.   The toolbar is responsible for the management of the available tools
  13.   and displays the icons for the available tools. Selection of the current
  14.   tool is handled here.
  15. }  
  16.  
  17. interface
  18.  
  19. uses PaintDef, Tools, WinTypes, WinProcs, WObjects;
  20.  
  21. type
  22.   { All available tools }
  23.   ToolName = (PenTool, LineTool,
  24.               FillTool,                    { ordering defines the layout of }
  25.               ORectTool, FRectTool,     { the display of icons }
  26.               OEllipseTool, FEllipseTool,
  27.               EraserTool, SelectTool);
  28.  
  29. const
  30.   MinTool = PenTool;
  31.   MaxTool = SelectTool;
  32.  
  33. type
  34.   PToolBar = ^TToolBar;
  35.   TToolBar = object(TWindow)
  36.     State: PState;            { communication among modules }
  37.     Tools: array[ToolName] of PPaintTool; { tools available }
  38.  
  39.     { Creation and destruction }
  40.     constructor Init(AParent: PWindowsObject; AState: PState);
  41.     destructor Done; virtual;
  42.  
  43.     { Utility }
  44.     procedure ToolSelect(Tool: ToolName);
  45.  
  46.     { Display } 
  47.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  48.  
  49.     { Window manager responses }
  50.     procedure WMLButtonDown(var Msg: TMessage);
  51.       virtual wm_First + wm_LButtonDown;
  52.   end;
  53.  
  54. implementation
  55.  
  56. { Create the actual toolbar and a new instance of each tool it is to contain.
  57. }
  58. constructor TToolBar.Init(AParent: PWindowsObject; AState: PState);
  59. begin
  60.   TWindow.Init(AParent, nil);
  61.   Attr.Style := ws_Child or ws_Visible;
  62.   State := AState;
  63.   Tools[PenTool] := New(PPenTool, Init(AState, 'PenTool', 'PenCursor'));
  64.   Tools[LineTool] := New(PLineTool, Init(AState, 'LineTool', 'PenCursor', False));
  65.   Tools[FillTool] := New(PFillTool, Init(AState, 'FillTool', 'FillCursor'));
  66.   Tools[ORectTool] := New(PRectTool, Init(AState, 'RectTool', 'CrossCursor', False));
  67.   Tools[FRectTool] := New(PRectTool, Init(AState, 'FillRectTool', 'CrossCursor', True));
  68.   Tools[OEllipseTool] := New(PEllipseTool, Init(AState, 'EllipseTool', 'CrossCursor', False));
  69.   Tools[FEllipseTool] := New(PEllipseTool, Init(AState, 'FillEllipseTool', 'CrossCursor', True));
  70.   Tools[EraserTool] := New(PEraserTool, Init(AState, 'EraserTool', 'EraserCursor'));
  71.   Tools[SelectTool] := New(PSelectTool, Init(AState, 'SelectTool', 'CrossCursor', False));
  72.   Tools[PenTool]^.Select;
  73. end;
  74.  
  75. { Destroy each tool instance befory dying.
  76. }
  77. destructor TToolBar.Done;
  78. var
  79.   Tool: ToolName;
  80. begin
  81.   for Tool := MinTool to MaxTool do Dispose(Tools[Tool], Done);
  82.   TWindow.Done;
  83. end;
  84.  
  85. { Deselect the current tool and select the new tool and update the display.
  86. }
  87. procedure TToolBar.ToolSelect(Tool: ToolName);
  88. begin
  89.   State^.PaintTool^.Deselect;
  90.   Tools[Tool]^.Select;
  91.   InvalidateRect(HWindow, nil, False);
  92. end;
  93.  
  94. { Paint the toolbar by painting the icon for each tool horizontally. The icon
  95.   for the currently selected tool is highlighted. Note that icons are 32x32,
  96.   but overlap by one pixel.
  97. }
  98. procedure TToolBar.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  99. var
  100.   I: Integer;        { Position of tool in row of icons }
  101.   Tool: ToolName;    { Current tool being drawn }
  102.   R: TRect;        { Coordinates of icon }
  103. begin
  104.   for Tool := MinTool to MaxTool do
  105.   begin
  106.     I := Ord(Tool);
  107.     DrawIcon(PaintDC, I * 31, 0, Tools[Tool]^.Icon);
  108.     
  109.     { Highlight currently selected tool }
  110.     if Tools[Tool] = State^.PaintTool then
  111.     begin
  112.       R.top := 1;
  113.       R.left := I * 31 + 1;
  114.       R.bottom := R.top + 30;
  115.       R.right := r.left + 30;
  116.       InvertRect(PaintDC, R);
  117.     end;
  118.   end;
  119. end;
  120.  
  121. { Select the tool whose icon is pressed.
  122. }
  123. procedure TToolBar.WMLButtonDown(var Msg: TMessage);
  124. var
  125.   Tool: ToolName;
  126. begin
  127.   Tool := ToolName(Msg.LParamLo div 31);
  128.   if Tool <= MaxTool then ToolSelect(Tool);
  129. end;
  130.  
  131. end.
  132.